package com.egame.manage.biz.server.gc;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

import cn.egame.common.exception.ExceptionCommonBase;
import cn.egame.common.model.PageData;
import cn.egame.interfaces.pu.AppParameter;

import com.egame.manage.biz.domain.TerminalGroupInfoBean;
import com.egame.manage.biz.server.ManageClient;
import com.egame.manage.biz.server.pu.ParameterServiceDao;

public class TerminalGroupServiceImpl {
	private static TerminalGroupServiceImpl instance = null;
	private static byte[] SyncRoot = new byte[1];
	private TerminalGroupServiceDao dao = null;
	
	public static TerminalGroupServiceImpl getInstance() throws ExceptionCommonBase {
		if (instance == null) {
			synchronized (SyncRoot) {
				if (instance == null) {
					instance = new TerminalGroupServiceImpl();
				}
			}
		}
		return instance;
	}
	
	public TerminalGroupServiceImpl() throws ExceptionCommonBase{
		super();
		dao = new TerminalGroupServiceDao();
	}
	
	public PageData pageTerminalGroupIds(int appId, long loginUserId,String name,int page,int rowsOfPage)throws RemoteException{
		List<Integer> ids = dao.listTerminalGroupIds(name, page, rowsOfPage);
		List<TerminalGroupInfoBean> beans = new ArrayList<TerminalGroupInfoBean>();
		for(Integer id : ids){
			AppParameter appParameter = ManageClient.getInstance().getAppParameterById(appId, loginUserId, id);
			TerminalGroupInfoBean bean = new TerminalGroupInfoBean();
			formatTerminalGroupInfoBean(bean, appParameter);
			beans.add(bean);
		}
		int total = dao.getTerminalGroupCount(name);
		PageData pd = new PageData(page, total, rowsOfPage);
		pd.setContent(beans);
		return pd;
	}

	/**
	 * 修改给定机型id的机型组id并且修改t_terminal_game_file_link中的机型组id
	 * @param terminalIds
	 * @param groupId 
	 * @throws ExceptionCommonBase
	 */
	public void updateTerminalGroupId(String[] terminalIds, int groupId) throws ExceptionCommonBase{
		dao.clearTerminalGroupIdInTerminal(groupId);
		dao.clearTerminalGroupIdInTerminalGameFileLink(groupId);
		dao.updateTerminalGroupIdInTerminal(terminalIds, groupId);
		dao.updateTerminalGroupIdInTerminalGameFileLink(terminalIds, groupId);
	}

	public TerminalGroupInfoBean getTerminalGroupInfoById(int appId,long loginUserId,int groupId)
			 throws ExceptionCommonBase{
		TerminalGroupInfoBean bean = new TerminalGroupInfoBean();
		try {
			AppParameter appParameter = ManageClient.getInstance().getAppParameterById(appId, loginUserId, groupId);
			formatTerminalGroupInfoBean(bean, appParameter);
		} catch (RemoteException e) {
			e.printStackTrace();
			throw ExceptionCommonBase.throwExceptionCommonBase(e);
		}
		return bean;
	}
	
	private  TerminalGroupInfoBean formatTerminalGroupInfoBean(TerminalGroupInfoBean bean, AppParameter appParameter) throws ExceptionCommonBase{
		if(appParameter!=null){
			bean.setId(appParameter.getId());
			bean.setName(appParameter.getName());
			bean.setRemark(appParameter.getRemark());
			bean.setRelatedCount(dao.getTerminalCountByGroupId(appParameter.getId()));
		}
		return bean;
	}

	/** 修改机型组信息,并修改其关联的机型 */
	public void updateTerminalGroupInfo(int appId, long loginUserId, TerminalGroupInfoBean bean,String[] toTerminalIds) throws ExceptionCommonBase{
		if(bean.getId() == 0){
			return;
		}
		try {
			AppParameter appParameter = ManageClient.getInstance().getAppParameterById(appId, loginUserId, bean.getId());
			appParameter.setName(bean.getName());
			appParameter.setRemark(bean.getRemark());
			ManageClient.getInstance().setAppParameter(appId, loginUserId, appParameter);
			//修改关联的机型信息
			if(toTerminalIds!=null){
				this.updateTerminalGroupId(toTerminalIds, bean.getId());
			}else{
				dao.clearTerminalGroupIdInTerminal(bean.getId());
				dao.clearTerminalGroupIdInTerminalGameFileLink(bean.getId());
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw ExceptionCommonBase.throwExceptionCommonBase(e);
		}
	}

	public int checkTerminalGroupNameExist(int id, String name) throws ExceptionCommonBase{
		return dao.checkTerminalGroupNameExist(id, name);
	}
	
	/** 删除机型组信息(依次删除t_terminal,t_terminal_game_file_link,t_parameter_app中的信息 */
	public void deleteTerminalGroup(int groupId) throws ExceptionCommonBase{
		dao.clearTerminalGroupIdInTerminal(groupId);
		dao.clearTerminalGroupIdInTerminalGameFileLink(groupId);
		dao.deleteTerminalGroup(groupId);
	}
	

}
